package com.lianniu.web.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.product.dao.ISkuProductDao;
import com.lianniu.web.product.model.Page;
import com.lianniu.web.product.model.SkuProductVo;
import com.lianniu.web.product.service.ISkuProductService;
import com.lianniu.web.util.StringUtil;
@Service
public class SkuProductServiceImpl implements ISkuProductService {
	
	private static final Logger logger = Logger.getLogger(SkuProductServiceImpl.class);
	
	@Autowired
	ISkuProductDao skuProductDao;

	@Override
	public BaseResult insertSkuProduct(SkuProductVo skuProduct) {
		logger.info("insertSkuProduct begin");
		logger.info("insertSkuProduct check data begin");
		logger.info("insertSkuProduct skuProduct"+JSON.toJSONString(skuProduct));
		return BaseResult.success(skuProductDao.insertSkuProduct(skuProduct));
	}

	@Override
	public BaseResult selectBySkuProductForPage(SkuProductVo skuProduct,Integer pageNum,String userid) {
		logger.info("selectBySkuProduct begin");
	    if (!StringUtil.isEmpty(skuProduct.getBuyerId())) {
	    	skuProduct.setBuyerId(skuProduct.getBuyerId().replace(" ", ""));
	    }
	    logger.info("selectBySkuProductForPage buyerId:" +skuProduct.getBuyerId());
	    if (!StringUtil.isEmpty(skuProduct.getShopProductId())) {
	    	skuProduct.setShopProductId(skuProduct.getShopProductId().replace(" ", ""));
	    }
	    logger.info("selectBySkuProductForPage ShopProductId:" +skuProduct.getShopProductId());
	    Map<String, Object> param = new HashMap<>();
		param.put("userid", userid);
		int count = skuProductDao.selectMyProductCount(param);
		Page page = new Page(count, pageNum);
		param.put("page", page);
	    List<SkuProductVo> list =null;
	    if (pageNum<=page.getPageSum()) {
	        list = skuProductDao.selectBySkuProducts(param);
        }
	    logger.info("selectBySkuProductForPage:  end");    
	    return BaseResult.success(list); 
	}

	@Override
	public BaseResult deleteById(String id) {
		logger.info("deleteById begin");
		return BaseResult.success(skuProductDao.deleteById(id));
	}

	@Override
	public BaseResult updateById(SkuProductVo skuProduct) {
		logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curProductBrand:" +JSON.toJSONString(skuProduct));
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(skuProductDao.updateById(skuProduct));
	}

	@Override
	public BaseResult deleteSkuProductBatch(String... id) {
		logger.info("deleteSkuProductBatch begin");
	    logger.info("deleteSkuProductBatch Ids:" +id.toString());
	    logger.info("deleteSkuProductBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteSkuProductBatch: input data check end");
	    
	    return BaseResult.success(skuProductDao.deleteSkuProductBatch(id));
	}

	@Override
	public BaseResult selectById(SkuProductVo skuProduct) {
		logger.info("selectById begin");
	    logger.info("selectById Id:" +skuProduct.getId());
	  
	    return BaseResult.success(skuProductDao.selectById(skuProduct.getId()));
	}

	@Override
	public BaseResult getSkuProductById(String id) {
		logger.info("getSkuProductById begin");
		return BaseResult.success(skuProductDao.getSkuProductById(id));
		
	}

    @Override
    public BaseResult updateTime(SkuProductVo skuProduct) {
      logger.info("updateTime begin");
        return BaseResult.success(skuProductDao.update(skuProduct));
    }
    /**
     * @Title: updateStatus 
     * @Description: 下架
     * @author: guoconglin
     * @param id
     * @return
     * @date: 2016年8月22日 上午9:48:47 
     * @return: BaseResult
     */
    @Override
    public BaseResult updateSkuProductStatus(String id) {
        logger.info("updateStatus begin");
        return BaseResult.success(skuProductDao.updateSkuProductStatus(id));
    }
    /**
     * @Title: updateBySkuId 
     * @Description: 上架
     * @author: guoconglin
     * @param id
     * @return
     * @date: 2016年8月22日 上午9:49:48 
     * @return: BaseResult
     */
    @Override
    public BaseResult updateSkuProduct(String id) {
        logger.info("updateBySkuId begin");
        return BaseResult.success(skuProductDao.updateSkuProduct(id));
    }

    @Override
    public BaseResult getOverPlusNumBySkuId(String id) {
        logger.info("getOverPlusNumBySkuId begin");
        double d = skuProductDao.getOverPlusNumBySkuId(id);
        logger.info("getOverPlusNumBySkuId end");
        return BaseResult.success(d);
    }

    @Override
    public BaseResult updateOverPlusNumBySkuId(double overPlusNum, String id) {
        logger.info("updateOverPlusNumBySkuId begin");
        int i = skuProductDao.reduceOverPlusNum(overPlusNum, id);
        logger.info("updateOverPlusNumBySkuId end");
        return BaseResult.success(i);
    }

    @Override
    public BaseResult addOverPlusNumBySkuId(double overPlusNum, String id) {
       logger.info("addOverPlusNumBySkuId begin");
       int i = skuProductDao.addOverPlusNumBySkuId(overPlusNum, id);
        return BaseResult.success(i);
    }

}
